/**
 * Copyright (C) 2010 David Simonek <dafesimonek@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dafe.partyband.gui.concerts;

import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.dafe.partyband.db.api.IConcert;
import org.dafe.partyband.db.api.queries.IConcertQuery;
import org.dafe.partyband.db.api.DbManager;
import org.dafe.partyband.gui.Utilities;
import org.jdesktop.swingx.JXCollapsiblePane.Direction;
import org.jdesktop.swingx.JXTitledPanel;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
//import org.openide.util.ImageUtilities;
import org.netbeans.api.settings.ConvertAsProperties;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.dafe.partyband.gui.concerts//Concert//EN",
autostore = false)
public final class ConcertTopComponent extends TopComponent {

    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "ConcertTopComponent";
    private ConcertHeaderPanel headerPanel;
    private PlaylistPanel playlistPanel;
    private ConcertActionsPanel actionsPanel;
    private JXTitledPanel durationsTitledPanel;

    private static final IConcertQuery concertQuery = DbManager.getBandManager().getQueriesLookup().lookup(IConcertQuery.class);

    public ConcertTopComponent() {
        this(null);
    }

    public ConcertTopComponent(IConcert concert) {
        initComponents();
        setName(NbBundle.getMessage(ConcertTopComponent.class, "CTL_ConcertTopComponent"));
        setToolTipText(NbBundle.getMessage(ConcertTopComponent.class, "HINT_ConcertTopComponent"));
//        setIcon(ImageUtilities.loadImage(ICON_PATH, true));

        headerPane.setDirection(Direction.UP);
        actionsPane.setDirection(Direction.RIGHT);

        playlistTitledPanel.setLeftDecoration(Utilities.createCollapsibleHandler(
                KeyEvent.VK_UP, 
                NbBundle.getMessage(ConcertTopComponent.class, "HINT_ShowHideHeader"),
                headerPane));
        playlistTitledPanel.setRightDecoration(Utilities.createCollapsibleHandler(
                KeyEvent.VK_RIGHT,
                NbBundle.getMessage(ConcertTopComponent.class, "HINT_ShowHideActions"),
                actionsPane));

        if (concert != null) {
            initViews(concert);
        }
    }

    private void initViews (IConcert concert) {
        headerPanel = new ConcertHeaderPanel(concert, this);
        headerPane.getContentPane().add(headerPanel);

        playlistPanel = new PlaylistPanel(concert);
        playlistTitledPanel.setContentContainer(playlistPanel);

        actionsPanel = new ConcertActionsPanel(concert, playlistPanel);
        actionsPane.getContentPane().add(actionsPanel);
        
        durationsTitledPanel = new JXTitledPanel(NbBundle.getMessage(ConcertTopComponent.class, "DurationsTitledPanel.title"));
        durationsTitledPanel.setContentContainer(new DurationsPanel(playlistPanel, concert));
        durationsPane.getContentPane().add(durationsTitledPanel);
        durationsTitledPanel.setRightDecoration(Utilities.createCollapsibleHandler(
                KeyEvent.VK_DOWN,
                NbBundle.getMessage(ConcertTopComponent.class, "HINT_ShowHideDurations"),
                durationsPane));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headerPane = new org.jdesktop.swingx.JXCollapsiblePane();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        playlistTitledPanel = new org.jdesktop.swingx.JXTitledPanel();
        actionsPane = new org.jdesktop.swingx.JXCollapsiblePane();
        durationsPane = new org.jdesktop.swingx.JXCollapsiblePane();

        setLayout(new java.awt.BorderLayout());
        add(headerPane, java.awt.BorderLayout.NORTH);

        jXPanel1.setLayout(new java.awt.BorderLayout());

        playlistTitledPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 5, 5, 1));
        playlistTitledPanel.setTitle(org.openide.util.NbBundle.getMessage(ConcertTopComponent.class, "ConcertTopComponent.playlistTitledPanel.title", new Object[] {})); // NOI18N
        jXPanel1.add(playlistTitledPanel, java.awt.BorderLayout.CENTER);
        jXPanel1.add(actionsPane, java.awt.BorderLayout.EAST);

        add(jXPanel1, java.awt.BorderLayout.CENTER);

        durationsPane.setDirection(org.jdesktop.swingx.JXCollapsiblePane.Direction.DOWN);
        add(durationsPane, java.awt.BorderLayout.SOUTH);
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXCollapsiblePane actionsPane;
    private org.jdesktop.swingx.JXCollapsiblePane durationsPane;
    private org.jdesktop.swingx.JXCollapsiblePane headerPane;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXTitledPanel playlistTitledPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        headerPanel.storeData();
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        if (headerPanel != null) {
            p.setProperty("location", headerPanel.getConcertLocation());
            p.setProperty("date", headerPanel.getConcertDate());
        }
    }

    Object readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        String location = p.getProperty("location");
        Date date = null;
        try {
            String stringDate = p.getProperty("date");
            if (stringDate != null) {
                date = SimpleDateFormat.getDateInstance().parse(stringDate);
            }
        } catch (ParseException ex) {
            Exceptions.printStackTrace(ex);
        }
        if (location != null && date != null &&
                !DbManager.getObjectContainer().ext().isClosed()) {
            IConcert concert = concertQuery.findConcerts(date, location).get(0);
            if (concert != null) {
                initViews(concert);
            }
        }

        return this;
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }
}
